package com.itheima.health.controller.mobile;

import com.itheima.health.constant.MessageConstant;
import com.itheima.health.constant.RedisMessageConstant;
import com.itheima.health.entity.Result;
import com.itheima.health.service.OrderService;
import com.itheima.health.utils.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

// 体检预约
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private OrderService orderService;

    @RequestMapping("/submitOrder.do")
    public Result submitOrder(@RequestBody Map<String, String> map) {
        // 1.前端提交的验证码
        String validateCode = map.get("validateCode");

        // 2.前端提交的手机号
        String telephone = map.get("telephone");

        // 3.获取Redis中的验证码
        String authCode = (String) stringRedisTemplate.opsForValue().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);

        // 4.校验短信验证码
        if (authCode == null || !authCode.equals(validateCode)) {
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }

        // 5.调用DAO务完成预约
        Result result = orderService.order(map);

        return result;
    }

    @PostMapping("findById.do")
    public Result findById(Integer id) {
        return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, orderService.findById(id));
    }
}